package enrique.kurso.tablet;

import java.util.ArrayList;
import java.util.List;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import enrique.kurso.R;
import enrique.kurso.lecionoj.Leciono01;
import enrique.kurso.tablet.fragments.Leciono01Fragment;
import enrique.kurso.util.ProgressView;

public class MenuFragment extends ListFragment {
	private ListAdapter m_adapter;
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
      
        List<String> options=new ArrayList<String>();
        options.add(getResources().getString(R.string.leciono1));
        options.add(getResources().getString(R.string.leciono2));
        options.add(getResources().getString(R.string.leciono3));
        options.add(getResources().getString(R.string.leciono4));
        options.add(getResources().getString(R.string.leciono5));
        options.add(getResources().getString(R.string.leciono6));
        options.add(getResources().getString(R.string.leciono7));
        options.add(getResources().getString(R.string.leciono8));
        options.add(getResources().getString(R.string.leciono9));
        options.add(getResources().getString(R.string.leciono10));
        options.add(getResources().getString(R.string.leciono11));
        options.add(getResources().getString(R.string.leciono12));
        
        this.m_adapter = new ListAdapter(this.getActivity(), R.layout.optionslist,options);
        setListAdapter(this.m_adapter);
        
	}
	

	  private class ListAdapter extends ArrayAdapter<String> {

		  private List<String> items;
		  private Context context;

		  public ListAdapter(Context context, int textViewResourceId, List<String> items) {
			  super(context, textViewResourceId, items);
			  this.items = items;
			  this.context = context;
		  }

		  public void setItems(List<String> items) {
			  this.items = items;
		  }
	        
		  @Override
		  public View getView(int position, View convertView, ViewGroup parent) {
			  View v = convertView;
			  if (v == null) {
				  LayoutInflater vi = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				  v = vi.inflate(R.layout.optionslist, null);
			  }
			  String titulo = items.get(position);
			  if (titulo!=null) {
				  TextView tt = (TextView) v.findViewById(R.id.toptext);
				  if (tt != null) {
					  tt.setText(titulo);
				  }	
				  int fallos=0;
				  int preguntas=0;
				  int respuestas=0;
				  ProgressView progress = (ProgressView) v.findViewById(R.id.exerciseProgress);
				  if(progress != null){
					  if(preguntas>0 && respuestas>0)
						  progress.setPercent(respuestas*100/(preguntas*4),100-900/(9+fallos));
					  else progress.setPercent(0,0);
				  }
			  }
			  return v;
		  }
	  }
	  
	  public void onListItemClick(ListView l, View v, int position, long id) {
		  FragmentManager fragmentManager = getFragmentManager();
		  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		  Leciono01Fragment fragment = new Leciono01Fragment();
		  fragmentTransaction.add(R.id.middle_pane, fragment,"leccionMenuFragment");
		  fragmentTransaction.commit();
	        
		 
	    }
}
